<template>
  <div>
    <div class="search-box">
      <router-link to="/recommend">
        <span><img src="../../assets/Search/30.png" alt="" /></span>
      </router-link>
      <!-- 搜索 -->
      <input
        type="text"
        placeholder="搜你想听"
        v-model="keywords"
        @change="getTopSearchFun"
      />
      <div class="collect">
        <span class="iconfont icon-search"></span>
      </div>
      <!-- 取消搜索 -->
      <div class="forkOff" v-if="fork" @click="offfork">
        <span><img src="../../assets/Search/3.png" alt="" /></span>
      </div>
    </div>

    <!-- 搜索结果 -->
    <div class="result" v-if="boolis">
      <ul>
        <li
          v-for="(item, index) in lisits"
          :key="index"
          @click="addByEnterKey(item.name, item.id)"
        >
          <div class="reslfet">
            <span>{{ item.name }}</span>
            <span>{{ item.ar[0].name }}<span>/</span>{{ item.al.name }}</span>
          </div>

          <div class="resright">
            <span class="iconfont icon-zanting1"></span>
          </div>
          <div class="dr"></div>
        </li>
      </ul>
    </div>

    <div v-if="isbool">
      <!-- 历史记录 -->
      <div class="history">
        <span @click="del()"
          ><img src="../../assets/Search/2.png" alt=""
        /></span>
        <h5>搜索历史</h5>
        <ul>
          <li
            v-for="(item, index) in defaulta"
            :key="index"
            @click="addget(item)"
          >
            {{ item }}
          </li>
        </ul>
      </div>

      <!-- 热门推荐 -->
      <div class="hot">
        <h3>热门推荐</h3>
        <ul>
          <li
            v-for="(item, index) in hotlis"
            :key="index"
            @click="getadd(item.first)"
          >
            <span>{{ index + 1 }}</span
            ><span>.</span><span>{{ item.first }}</span>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { getTopSearch, getTopHot } from "../../api/search.js";

export default {
  data() {
    return {
      keywords: "", //搜索关键词
      isbool: true,
      boolis: false,
      fork: false,
      lisits: [],
      defaulta: [],
      hotlis: [],
    };
  },
  methods: {
    getTopSearchFun() {
      // 搜索
      getTopSearch(this.keywords).then((data) => {
        console.log(data);
        if (this.keywords.length > 0) {
          this.isbool = false;
          this.boolis = true;
          this.fork = true;
        } else {
          this.isbool = true;
          this.boolis = false;
          this.fork = false;
        }
        this.lisits = data.result.songs;
        // console.log(this.lisits);
      });
    },

    getTopHotFun() {
      getTopHot().then((data) => {
        // console.log(data);
        this.hotlis = data.result.hots;
      });
    },

    addByEnterKey(it, id) {
      this.defaulta.push(it);
      // console.log("歌曲ID",id);
      this.$parent.getPlayerId(id);
    },
    // 搜索热门推荐
    getadd(val) {
      this.keywords = val;
      this.getTopSearchFun();
    },
    // 删除历史记录
    del() {
      this.defaulta.splice(0, this.defaulta.length);
    },
    //  搜索历史记录
    addget(val) {
      this.keywords = val;
      this.getTopSearchFun();
    },
    //  取消搜索
    offfork() {
      // console.log(111);
      this.keywords = "";
      this.boolis = false;
      this.isbool = true;
      this.fork = false;
    },
  },

  created() {
    this.getTopHotFun();
  },
};
</script>

<style lang="less" scoped>
.search-box {
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: space-evenly;
  align-items: center;
  position: relative;

  span {
    display: inline-block;
    width: 25px;
    height:25px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  input {
    width: 85%;
    height: 50%;
    border-radius: 20px;
    box-sizing: border-box;
    border: none;
    background: #ebecec;
    font-size: 10px;
    padding-left: 50px;
  }
  .collect {
    position: absolute;
    top: 31px;
    left: 18%;
  }
  .forkOff {
    position: absolute;
    top: 32%;
    right: 5%;
  }
}
.history {
  width: 100%;
  box-sizing: border-box;
  padding: 12px;
  position: relative;
  span {
    position: absolute;
    right: 15px;
    display: inline-block;
    width: 20px;
    height: 20px;
    img {
      width: 100%;
      height: 100%;
    }
  }

  h5 {
    font-weight: bold;
  }

  ul {
    width: 100%;
    height: auto;
    margin-top: 10px;
    li {
      box-sizing: border-box;
      text-align: center;
      display: inline;
      margin: 10px;
      padding: 3px;
      font-size: 13px;
      border: 1px solid gray;
      border-radius: 10px;
      line-height: 2;
    }
  }
}

.hot {
  width: 100%;
  box-sizing: border-box;
  padding: 6px 10px 5px 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  h3 {
    font-size: 14px;
    color: black;
    font-weight: bold;
  }
  ul {
    width: 100%;
    margin-top: 10px;
    li {
      margin-top: 5px;
      span:nth-child(1) {
        display: inline-block;
        width: 25px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        margin-left: 3px;
        margin-right: 3px;
      }
      span:nth-child(2) {
        display: inline-block;
        height: 20px;
        line-height: 20px;
        text-align: center;
        margin-left: 3px;
        margin-right: 10px;
      }
      span:nth-child(3) {
        display: inline-block;
        height: 20px;
        line-height: 20px;
        text-align: center;
        margin-left: 3px;
        margin-right: 10px;
        color: gray;
      }
      span:nth-child(4) {
        display: inline-block;
        height: 20px;
        line-height: 20px;
        text-align: center;
        margin-left: 3px;
        margin-right: 10px;
        font-size: 13px;
      }
    }
    li:nth-child(1),
    li:nth-child(2),
    li:nth-child(3) {
      position: relative;
      span:nth-child(1) {
        color: red;
      }
      &::after {
        position: absolute;
        top: 10%;
        content: "HOT";
        color: white;
        font-size: 9px;
        background: red;
        border-radius: 5px;
        font-style: oblique;
      }
    }
  }
}

.result {
  width: 100%;
  ul {
    li {
      width: 100%;
      position: relative;
      box-sizing: border-box;
      padding: 6px 10px 5px 12px;
      display: flex;
      .reslfet {
        width: 90%;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        span:nth-child(1) {
          display: inline-block;
          height: 60%;
          margin-left: 3px;
          margin-right: 3px;
        }
        span:nth-child(2) {
          display: inline-block;
          height: 40%;
          color: gray;
          font-size: 9px;
          margin-left: 3px;
          margin-right: 10px;
          span {
            font-size: 12px;
            font-weight: bold;
          }
        }
      }
      .resright {
        width: 10%;
        height: 100%;
        float: right;
        padding-top: 11px;
      }
      .dr {
        width: 96%;
        position: absolute;
        bottom: 0px;
        right: 0px;
        border-bottom: 1px solid rgb(185, 184, 184);
      }
    }
  }
}
</style>